package com.linkstec.java8;

import java.util.HashSet;
import java.util.Set;

public class Cpu100 {
	static Set<Thread> set = new HashSet<Thread>();

	@SuppressWarnings("deprecation")
	public static void main(String[] args) {
		int num = Runtime.getRuntime().availableProcessors();
		//1.cpu有多少核起几个线程
		for (int i = 0; i<num; i++) {
			Thread thread = new Thread(new Runnable() {
				@Override
				public void run() {
					while(true){
						//2.消耗cpu
					}
				}
			});
			set.add(thread);
			thread.start();
			try {
				//3.休息1000再加一个线程
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		while (true) {
			for (Thread thread : set) {
				thread.suspend();
				try {
					//4.休息1000再暂停一个线程
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			for (Thread thread : set) {
				thread.resume();
				try {
					//5.休息1000再恢复一个线程
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}